package com.kuangjia.conferenceroomrental.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kuangjia.conferenceroomrental.entity.Room;
import com.kuangjia.conferenceroomrental.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

/**
 * 会议室控制器
 */
@RestController
@RequestMapping("/rooms")
public class RoomController {
    
    private final RoomService roomService;
    
    @Autowired
    public RoomController(RoomService roomService) {
        this.roomService = roomService;
    }
    
    /**
     * 创建会议室
     */
    @PostMapping
    public ResponseEntity<Room> createRoom(@RequestBody Room room) {
        Room createdRoom = roomService.createRoom(room);
        return ResponseEntity.status(HttpStatus.CREATED).body(createdRoom);
    }
    
    /**
     * 根据ID获取会议室
     */
    @GetMapping("/{id}")
    public ResponseEntity<Room> getRoomById(@PathVariable Long id) {
        Room room = roomService.getRoomById(id);
        if (room == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(room);
    }
    
    /**
     * 获取所有会议室
     */
    @GetMapping
    public ResponseEntity<List<Room>> getAllRooms() {
        List<Room> rooms = roomService.getAllRooms();
        return ResponseEntity.ok(rooms);
    }
    
    /**
     * 分页获取所有会议室
     */
    @GetMapping("/page")
    public ResponseEntity<Map<String, Object>> getRoomsByPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Room> page = new Page<>(current, size);
        IPage<Room> pageResult = roomService.getRoomsByPage(page);
        
        Map<String, Object> result = new HashMap<>();
        result.put("records", pageResult.getRecords());
        result.put("total", pageResult.getTotal());
        result.put("size", pageResult.getSize());
        result.put("current", pageResult.getCurrent());
        result.put("pages", pageResult.getPages());
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 获取所有可用会议室
     */
    @GetMapping("/available")
    public ResponseEntity<List<Room>> getAvailableRooms() {
        List<Room> rooms = roomService.getAvailableRooms();
        return ResponseEntity.ok(rooms);
    }
    
    /**
     * 分页获取所有可用会议室
     */
    @GetMapping("/available/page")
    public ResponseEntity<Map<String, Object>> getAvailableRoomsByPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Room> page = new Page<>(current, size);
        IPage<Room> pageResult = roomService.getAvailableRoomsByPage(page);
        
        Map<String, Object> result = new HashMap<>();
        result.put("records", pageResult.getRecords());
        result.put("total", pageResult.getTotal());
        result.put("size", pageResult.getSize());
        result.put("current", pageResult.getCurrent());
        result.put("pages", pageResult.getPages());
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 根据容量获取会议室
     */
    @GetMapping("/capacity/{capacity}")
    public ResponseEntity<List<Room>> getRoomsByCapacity(@PathVariable Integer capacity) {
        List<Room> rooms = roomService.getRoomsByCapacity(capacity);
        return ResponseEntity.ok(rooms);
    }
    
    /**
     * 分页根据容量获取会议室
     */
    @GetMapping("/capacity/{capacity}/page")
    public ResponseEntity<Map<String, Object>> getRoomsByCapacityPage(
            @PathVariable Integer capacity,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Room> page = new Page<>(current, size);
        IPage<Room> pageResult = roomService.getRoomsByCapacityPage(page, capacity);
        
        Map<String, Object> result = new HashMap<>();
        result.put("records", pageResult.getRecords());
        result.put("total", pageResult.getTotal());
        result.put("size", pageResult.getSize());
        result.put("current", pageResult.getCurrent());
        result.put("pages", pageResult.getPages());
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 更新会议室
     */
    @PutMapping("/{id}")
    public ResponseEntity<Room> updateRoom(@PathVariable Long id, @RequestBody Room room) {
        Room existingRoom = roomService.getRoomById(id);
        if (existingRoom == null) {
            return ResponseEntity.notFound().build();
        }
        
        room.setId(id);
        Room updatedRoom = roomService.updateRoom(room);
        return ResponseEntity.ok(updatedRoom);
    }
    
    /**
     * 删除会议室
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteRoom(@PathVariable Long id) {
        Room existingRoom = roomService.getRoomById(id);
        if (existingRoom == null) {
            return ResponseEntity.notFound().build();
        }
        
        roomService.deleteRoom(id);
        return ResponseEntity.noContent().build();
    }
} 